﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyCommerce.ManagementBase.Common.Base.Common;
using MyCommerce.Presentation.UsersAndSecurity.Presenters;
using MyCommerce.Presentation.UsersAndSecurity.IViews;
using Innova.Microfinanzas.BusinessEntities;
using Innova.FoundationFramework.DataTypes;

namespace MyCommerce.Windows.UsersAndSecurity.Views
{
    public partial class EmployeeAdministrationView : BaseForm, IEmployeeAdministrationView
    {
        private EmployeeAdministrationPresenter pPresenter;

        public EmployeeAdministrationView()
        {
            InitializeComponent();
            pPresenter = new EmployeeAdministrationPresenter(this);
        }

        #region IEmployeeAdministrationView Members

        public long UserId
        {
            get
            {
                return Context.UserId;
            }
        }

        public List<Employee> ColItemsList
        {
            get
            {
                return (List<Employee>)gridEmplyees.DataSource;
            }
            set
            {
                gridEmplyees.DataSource = value;
                gridEmplyees.DataBind();
            }
        }

        public Employee ObjItemSelected
        {
            get
            {
                if (gridEmplyees.ActiveRow != null
                    && gridEmplyees.ActiveRow.Selected)
                    return (Employee)gridEmplyees.ActiveRow.ListObject;
                return null;
            }
        }

        public bool InvokeEditionView(Employee objItemToEdit)
        {
            bool result = false;
            Command commandForm = Context.GetCommand("EmployeeCreateEdit");
            object formResult = Context.InvoqueCommandWithParams(commandForm, objItemToEdit, true);
            if (formResult != null)
                result = Convert.ToBoolean(formResult);
            return result;
        }

        public event GenericEventHandler<long> OnLoadView;

        public event EventHandler OnCreate;

        public event GenericEventHandler<Employee> OnEdit;

        public event GenericEventHandler<Employee> OnDelete;

        #endregion

        private void tsCloseButtonToolStrip_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void tsDeleteButtonToolStrip_Click(object sender, EventArgs e)
        {
            try
            {
                if (ObjItemSelected != null)
                {
                    if (Context.ShowQuestionMessageBox("¿Desea eliminar a este empleado?", "Eliminar empleado") == System.Windows.Forms.DialogResult.Yes)
                    {
                        if (OnDelete != null)
                        {
                            OnDelete(this, new GenericEventArgs<Employee>(ObjItemSelected));
                            Context.ShowInformationMessageBox("El empleado se eliminó correctamente.", "Eliminar empleado");
                        }
                    }
                }
                else
                    Context.ShowInformationMessageBox("Debe seleccionar un empleado para poder eliminarlo.", "Eliminar empleado");
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }
        }

        private void tsEditButtonToolStrip_Click(object sender, EventArgs e)
        {
            if (ObjItemSelected != null)
            {
                if (OnEdit != null)
                    OnEdit(this, new GenericEventArgs<Employee>(ObjItemSelected));
            }
            else
                Context.ShowInformationMessageBox("Debe seleccionar un empleado para poder eliminarlo.", "Eliminar empleado");
        }

        private void tsNewButtonToolStrip_Click(object sender, EventArgs e)
        {
            if (OnCreate != null)
                OnCreate(this, new EventArgs());
        }

        private void EmployeeAdministrationView_Load(object sender, EventArgs e)
        {
            if (OnLoadView != null)
                OnLoadView(this, new GenericEventArgs<long>(Context.UserId));
        }
    }
}
